<template>
	<view class="container">
		<view class="group-box">
			<view class="ticket-list">
				<view v-if="listData.length===0" class="nodata">
					<text class="icon iconfont icon-ticket"></text>
					<text class="text">暂无可用优惠券...</text>
				</view>
				<!--循环.开始-->
				<view v-if="listData.length>0" class="list-wrap">
					<view v-for="(item,index) in listData" :key="index" class="list-box">
						<view class="left">
							<text class="price">{{item.shopCoupon.amount}}</text>
						</view>
						<view class="center">
							<view class="title">
								<text class="label" v-if="item.shopCoupon.useType==1">商品券</text>
								<text class="label" v-else>店铺券</text>
								<text>{{item.shopCoupon.title}}</text>
							</view>
							<text class="time">可用时间 {{item.shopCoupon.enableTime}}</text>
							<text class="time">有效期至 {{item.shopCoupon.endTime}}</text>
						</view>
						<view class="right">
							<text v-if="item.isUse==1" class="btn active">已使用</text>
							<text v-else class="btn">未使用</text>
						</view>
					</view>
				</view>
				<!--循环.结束-->
				<uni-load-more v-if="listData.length>0" :status="loadingType" @clickLoadMore="loadData('add')"></uni-load-more>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadingType: "more", //加载状态
				totalCount: 0, //总数量
				totalPages: 0, //总页数
				pageSize: 10, //每页显示条数
				pageIndex: 1, //当前页码
				listData: [], //数据列表
			}
		},
		methods: {
			//初始化数据,带下滑刷新和上滑加载
			loadData(type='add', loading){
				let _this = this;
				//上滑加载更多
				if(type === 'add'){
					if(_this.loadingType === 'nomore'){
						return;
					}
					_this.loadingType = 'loading';
				}
				//下滑刷新
				else{
					_this.pageIndex=1;
					_this.listData=[];
					_this.loadingType="more";
				}
				//加载列表
				_this.$api.request({
					url: `/account/coupon/history?pageSize=${_this.pageSize}&pageIndex=${_this.pageIndex}`,
					method: "GET",
					load: true
				}).then(res=>{
					let pageInfo = JSON.parse(res.header["x-pagination"]);
					_this.totalCount = pageInfo.totalCount;
					_this.totalPages = pageInfo.totalPages;
					let listData = res.data;
					console.log(listData);
					_this.listData = _this.listData.concat(listData);
				}).catch((err)=>{
					_this.loadingType="nomore";
					uni.stopPullDownRefresh();
				});
				
				//判断是否还有下一页
				if(_this.pageIndex >= _this.totalPages){
					_this.loadingType="nomore";
				}else{
					_this.pageIndex++;
					_this.loadingType="more";
				}
				//下滑隐藏动画及上滑事件
				if(type === 'refresh'){
					if(loading == 1){
						uni.hideLoading()
					}else{
						uni.stopPullDownRefresh();
					}
				}
			},
		},
		//页面初始化
		onLoad(options){
			this.loadData('add');
		},
		//下滑刷新
		onPullDownRefresh(){
			this.loadData('refresh');
		},
		//上滑加载更多
		onReachBottom(){
			this.loadData('add');
		}
	}
</script>

<style lang="scss">
	.ticket-list{
		box-sizing: border-box;
		border-radius: 20rpx 20rpx 0 0;
		.nodata{
			position: absolute;
			left: 50%;
			top: 50%;
			margin: -200rpx auto auto -150rpx; 
			display: flex;
			justify-content: center;
			flex-direction: column;
			text-align: center;
			.icon{
				color: #ebebeb;
				font-size: 200rpx;
			}
			.text{
				margin-top: 24rpx;
				color: #ccc;
				font-size: 30rpx;
				line-height: 48rpx;
			}
		}
		.list-wrap{
			margin: -24rpx 0 24rpx;
			max-height: calc(100vh - 450rpx);
			.list-box{
				display: flex;
				justify-content: space-between;
				align-items: center;
				position: relative;
				margin-top: 24rpx;
				padding: 28rpx;
				border-radius: 20rpx;
				background: #fff;
				&:before{
					content: "";
					display: block;
					position: absolute;
					top: 50%;
					left: -24rpx;
					margin-top: -20rpx;
					width: 40rpx;
					height: 40rpx;
					background: #f5f5f5;
					border-radius: 40rpx;
					overflow: hidden;
				}
				.left{
					.price{
						color: #ff5500;
						font-size: 48rpx;
						font-weight: bold;
						&::before{
							content: "￥";
							font-size: 28rpx;
							font-weight: 500;
						}
					}
				}
				.center{
					flex-grow: 1;
					padding: 0 24rpx;
					line-height: 40rpx;
					.title{
						display: block;
						color: #333;
						font-size: 28rpx;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
						.label{
							position: relative;
							display: inline-block;
							margin-right: 6rpx;
							padding: 0 8rpx;
							color: #fff;
							font-size: 20rpx;
							height: 32rpx;
							line-height: 32rpx;
							background: #f03446;
							border-radius: 6rpx;
						}
					}
					.time{
						display: block;
						padding-top: 6rpx;
						color: #999;
						font-size: 24rpx;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}
				.right{
					.btn{
						padding: 20rpx 0 20rpx 20rpx;
						color: #ff5500;
						font-size: 28rpx;
						border-left: 2rpx dotted rgba($color: #ff5500, $alpha: 0.3);
						white-space: nowrap;
						text-overflow: ellipsis;
						&.active{
							color: #ccc;
						}
					}
				}
			}
		}
	}
</style>
